package com.teyao.teyao_back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teyao.teyao_back.config.myPage;
import com.teyao.teyao_back.entity.ProductViewWrapper;
import com.teyao.teyao_back.service.ProductViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/productView")
public class ProductViewController {
    @Autowired
    private ProductViewService productViewService;

    @GetMapping("/selectAll")
    public myPage selectAll(Integer currentPage, Integer size) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ProductViewWrapper> page = new Page(myPage.getCurrentPage(), size);
        IPage result = productViewService.page(page);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    @GetMapping("/all")
    public List<ProductViewWrapper> all() {
        return productViewService.list();
    }

    //商品名
    @GetMapping("/selectByProductName")
    public myPage selectByProductName(Integer currentPage, String productName) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ProductViewWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ProductViewWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name", productName);
        IPage result = productViewService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //通用名
    @GetMapping("/selectByNickName")
    public myPage selectByNickName(Integer currentPage, String nickName) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ProductViewWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ProductViewWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("nick_name", nickName);
        IPage result = productViewService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //供应商
    @GetMapping("/selectBySuppliers")
    public myPage selectBySuppliers(Integer currentPage, String suppliers) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ProductViewWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ProductViewWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("suppliers", suppliers);
        IPage result = productViewService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //生产批号
    @GetMapping("/selectByClass")
    public myPage selectByClass(Integer currentPage, String classs) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ProductViewWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ProductViewWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("classs", classs);
        IPage result = productViewService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //剂型
    @GetMapping("/selectByType")
    public myPage selectByType(Integer currentPage, String type) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ProductViewWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<ProductViewWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("type", type);
        IPage result = productViewService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    //    不定查询
    @GetMapping("/myQuery")
    public myPage MyQuery(Integer currentPage, Integer size, ProductViewWrapper product) {
        System.out.println(product + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
        return productViewService.MyQuery(currentPage, size, product);
    }
}
